import {
    packApiUrl
} from "./common.js"
import {
    useUserStore
} from '@/stores/index.js';

// 获取认证头部
function getAuthHeader() {
    const userStore = useUserStore();
    let header = {};
    if (userStore.user.token) {
        header['authentication'] = userStore.user.token;
    } else if (userStore.token) {
        header['authentication'] = userStore.token;
    }
    return header;
}

// 处理登录失效
function handleLoginFailure() {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title: "错误提示",
            content: "登录失效，是否重新登录。",
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    uni.login({
                        "provider": "weixin",
                        "onlyAuthorize": true,
                        success: (loginRes) => {
                            const userStore = useUserStore();
                            userStore.getUserInfo(loginRes).then(() => {
                                uni.reLaunch({
                                    url: '/pages/index/index'
                                });
                                resolve();
                            });
                        },
                        fail: (err) => {
                            uni.showToast({
                                title: "登录失败",
                                icon: "none"
                            });
                            reject(err);
                        }
                    });
                   
                }
                 
            }
        });
    });
}

// 普通请求方法
export function request(config = {}) {
    let {
        url,
        data = {},
        method = "GET",
        header = {}
    } = config;
    url = packApiUrl(url);
    header = { ...header, ...getAuthHeader() };

    return new Promise((resolve, reject) => {
        uni.request({
            url,
            data,
            method,
            header,
            success: res => {
                if (res.data.code === 1) {
                    resolve(res.data);
                } else if (res.statusCode === 401) {
                    handleLoginFailure().then(resolve).catch(reject);
                } else {
                    uni.showToast({
                        title: res.data.msg,
                        icon: "none"
                    });
                    reject(res.data);
                }
            },
            fail: err => {
                reject(err);
            }
        });
    });
}

// 上传头像
export function uploadFile(config = {}) {
    let {
        url,
        filePath,
        name,
        header = {}
    } = config;
    url = packApiUrl(url);
    header = { ...header, ...getAuthHeader() };

    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url,
            filePath,
            name,
            header,
            success: res => {
                // console.log("uploadFile", res);
                // 假设 res 是你的返回结果
                const parsedData = JSON.parse(res.data);
                // console.log("uploadFile", parsedData);
                if (parsedData.code === 1) {
                    resolve(parsedData)
                } else if (res.statusCode === 401) {
                    handleLoginFailure().then(resolve).catch(reject);
                } else {
                    uni.showToast({
                        title: parsedData.msg,
                        icon: "none"
                    });
                    reject(parsedData);
                }
            },
            fail: err => {
                console.log("uploadFile", err);
                reject(err);
            }
        });
    });
}
